package coml.jiyun.hp.reviewtopic;


import java.util.List;
import coml.jiyun.hp.reviewtopic.base.BaseApp;
import coml.jiyun.hp.reviewtopic.bean.HomeBeanTwo;
import coml.jiyun.hp.reviewtopic.db.DaoMaster;
import coml.jiyun.hp.reviewtopic.db.DaoSession;
import coml.jiyun.hp.reviewtopic.db.HomeBeanTwoDao;

public class DbUtil {

    private static DbUtil dbUtil;
        private static HomeBeanTwoDao homeBeanTwoDao;
    private static HomeBeanTwo homeBeanTwo;

    public static  DbUtil dbUtil(){
            if (dbUtil == null){
                synchronized (DbUtil.class){
                    if (dbUtil==null){
                        dbUtil = new DbUtil();
                    }
                }
            }
            return dbUtil;
        }

        public DbUtil(){
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(BaseApp.getInstance(), "student.db");

            DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());

            DaoSession daoSession = daoMaster.newSession();

           homeBeanTwoDao = daoSession.getHomeBeanTwoDao();

        }
        public  void insert(HomeBeanTwo homeBeanTwo){
            if (isHas(homeBeanTwo)){
                return;
            }
            homeBeanTwoDao.insertOrReplace(homeBeanTwo);
        }


     public List<HomeBeanTwo> query(){
            return homeBeanTwoDao.queryBuilder().list();
        }

        public void delete(HomeBeanTwo twoBean){
            if (isHas(twoBean)){
                homeBeanTwoDao.delete(twoBean);
            }
        }

        private static boolean isHas(HomeBeanTwo homeBeanTwo) {
            List<HomeBeanTwo> list = homeBeanTwoDao.queryBuilder().where(HomeBeanTwoDao.Properties.Title.eq(homeBeanTwo.getTitle())).list();
            if (list!=null&&list.size() > 0 ){
                return  true;
            }
            return  false;
        }
}
